package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreSpecialAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreSpecialInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * 精彩专栏界面
 * Created by lanouhn on 16/9/18.
 */
public class MoreSpecialActivity extends AppCompatActivity {

    private ImageView back;
    private MoreSpecialInfo info;
    private MoreSpecialAdapter adapter;
    private ListView mListView;
    private String url = " http://lol.zhangyoubao.com/apis/rest/TopicsService/all?page=1&i_=JfoZvarCyLfJIqs9SRFolapk" +
            "21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1474161094633&p_=16244&" +
            "v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=" +
            "TopicsService.all&time=1474161094&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion" +
            "=v1.0&nonce=908520&secretSignature=CKatZmOqhonjVMjEKHLaN3Y9PoE=";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_special);
        initView();
        initEvent();
        getSpecialData();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.more_special_listView);
        back = (ImageView) findViewById(R.id.more_recreation_back);
    }

    private void initEvent() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MoreSpecialActivity.this, MoreSpecialSecondActivity.class);
                intent.putExtra("name", info.getData().get(position).getName());
                intent.putExtra("pic", info.getData().get(position).getPic_url());
                intent.putExtra("topic_id", info.getData().get(position).getId());
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getSpecialData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Toast.makeText(MoreSpecialActivity.this, "连接失败！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                info = gson.fromJson(result, MoreSpecialInfo.class);
                if (info.getData().size() > 0) {
                    handler.sendEmptyMessage(1);
                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(MoreSpecialActivity.this, "数据获取失败！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    adapter = new MoreSpecialAdapter(MoreSpecialActivity.this, info);
                    mListView.setAdapter(adapter);
                    break;
            }
        }
    };
}
